package com.dangf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



import com.dangf.entity.Article;
import com.dangf.service.ArticleService;

/**
 * 文章管理 控制器
 * @author Hello_海生
 * @date 2014年3月27日
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	//发布文章
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> insert(Article article){
		Map<String, Object> map = new HashMap<String, Object>();
		if(article.getTitle()!=null && !"".equals(article.getTitle()) && article.getUserId()!=null){
			long id = (Long) articleService.save(article);
			if(id!=0){
				Article a = articleService.findById(id);
				map.put("message", "恭喜你，文章发布成功！");
				map.put("static","success");
				map.put("article",a);
				}else{
					map.put("message", "系统错误，发布失败！");
					map.put("static","error");
				}
		}else{
			map.put("message", "发布失败，请输入文章标题或发布者Id。");
			map.put("static","error");
		}
		return map;
	}
	
	//查询文章信息
	@RequestMapping("/show")
	@ResponseBody
	public Map<String, Object> show(long id){
		Map<String, Object> map = new HashMap<String, Object>();
		if(id!=0){
			Article a= articleService.findById(id);
			if(a!=null){
				map.put("message", "信息查询成功！");
				map.put("static","success");
				map.put("user",a);
			}else{
				map.put("message", "查询失败，此此文章  不存在。");
				map.put("static","error");
			}
		}else{
			map.put("message", "查询失败，请输入文章Id");
			map.put("static","error");
		}
		return map;
	}
	
	//查询所有信息
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(int start,int count){
		Map<String, Object> map = new HashMap<String, Object>();
		if(count!=0){
			List<Article> articles= articleService.findByPage(start, count);
			if(articles!=null){
				map.put("message", "信息查询成功！");
				map.put("static","success");
				map.put("articles",articles);
			}else{
				map.put("message", "查询失败，不知出何错误！");
				map.put("static","error");
			}
		}else{
			map.put("message", "查询失败，请入起始数量和结束数量");
			map.put("static","error");
		}
		return map;
	}
	
	//查询所有信息
	@RequestMapping("/type")
	@ResponseBody
	public Map<String, Object> typeList(long id,int start,int count){
		Map<String, Object> map = new HashMap<String, Object>();
		if(count!=0 && id!=0){
			List<Article> articles= articleService.findArticleListByParams(id, start, count);
			if(articles!=null){
				map.put("message", "信息查询成功！");
				map.put("static","success");
				map.put("articles",articles);
			}else{
				map.put("message", "查询失败，不知出何错误！");
				map.put("static","error");
			}
		}else{
			map.put("message", "查询失败，请入起始数量和结束数量");
			map.put("static","error");
		}
		return map;
	}
}
